package com.hsxshop.controller;

import com.hsxshop.model.ISupply;
import com.hsxshop.model.Pagination;
import com.hsxshop.model.User;
import com.hsxshop.service.BaseEntityService;
import com.hsxshop.service.ISupplyService;
import com.hsxshop.service.UserService;
import com.hsxshop.util.*;
import lombok.extern.java.Log;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangchongya on 17/7/17.
 */
@RestController
@RequestMapping(value = "/isupply/")
@Log
public class ISupplyController {

    @Resource(name = "isupplyService")
    private ISupplyService isupplyService;

    @Resource(name = "userService")
    private UserService userService;

    @Resource(name = "baseEntityService")
    private BaseEntityService baseEntityService;

    @RequestMapping(value = "list", method = RequestMethod.POST,produces= MediaType.APPLICATION_JSON_UTF8_VALUE,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String index(@RequestBody Map requestMap  ){
        Pagination pagination= PaginationTool.getPagination(requestMap);

        String phone=ParamConvertTool.getParamStr(requestMap,"phone");
        String beginDisplayTime=ParamConvertTool.getParamStr(requestMap,"beginDisplayTime");
        String endDisplayTime=ParamConvertTool.getParamStr(requestMap,"endDisplayTime");

        String whereStr="";
        if(null!=requestMap.get("state")){
            int state=ParamConvertTool.getParamInt(requestMap,"state");
            if(state!=-2) {
                whereStr = " and state=" + state;
            }
        }
        if(!"".equals(phone)){
            User whereU= userService.getEntity(" from User where phone=?0",phone);
            if(null!=whereU){
                whereStr=" and userId="+whereU.getId();
            }else{
                return JsonTool.returnObjectJsonErrorMsg("该手机不存在");
            }
        }
        if(!"".equals(beginDisplayTime)){
            whereStr=" and displayTime>"+beginDisplayTime;
        }
        if(!"".equals(endDisplayTime)){
            whereStr=" and displayTime<"+endDisplayTime;
        }
        List<ISupply> list=isupplyService.findByHQLWithPagination(pagination, SqlTool.returnSql("new ISupply( id,serialNum, userId, title, fishName, fishInfo, quantity, unit, unitWeight, price, tradePlace, townId, createTime, updateTime, displayTime, expireTime, state, generateOrder, displayType, remark,images,lowBuyQuantity,storage)", "ISupply", " where displayTime<?0"+whereStr, "order by displayTime desc"), DateTool.getStringDate());
        //获取当前时间
        List returnLists=new ArrayList();
        for (ISupply iSupply:list) {
            Map isMap=new HashMap();
            isMap.put("id",iSupply.getId());
            //查找用户手机
            User u=userService.getEntity(iSupply.getUserId());
            isMap.put("user_phone",ParamConvertTool.getUserPhone(u));
            isMap.put("title",iSupply.getTitle());
            isMap.put("fishInfo",iSupply.getFishInfo());

            if(!"元".equals(iSupply.getUnit())){
                isMap.put("quantity",iSupply.getQuantity()+iSupply.getUnit()+"("+iSupply.getUnitWeight()+"斤/"+iSupply.getUnit()+")");
            }else{
                isMap.put("quantity",iSupply.getQuantity()+iSupply.getUnit());
            }
            isMap.put("priceInfo",iSupply.getPrice()/100.0+"元/"+iSupply.getUnit());
            isMap.put("displayTime",iSupply.getDisplayTime());
            isMap.put("state",iSupply.getState());
            isMap.put("storage",iSupply.getStorage());
            returnLists.add(isMap);
        }
        return JsonTool.returnListJsonSuccess(returnLists,pagination);
    }

    @RequestMapping(value = "detail", method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String detail(@RequestBody Map requestMap){

        Integer id=(Integer)requestMap.get("id");
        ISupply iSupply=isupplyService.getEntity(id);
        if(null==iSupply){
            return JsonTool.returnObjectJsonErrorMsg("该供应不存在");
        }
        Map map=new HashMap();
        //查找用户手机
        String user_phone="";
        User u=userService.getEntity(iSupply.getUserId());
        String name="".equals(u.getName()) ? u.getNickname() : u.getName();
        if("".equals(name)){
            user_phone=u.getPhone();
        }else{
            user_phone=u.getPhone()+"("+name+")";
        }
        map.put("id",iSupply.getId());
        map.put("title",iSupply.getTitle());
        map.put("user_phone",user_phone);
        map.put("fishInfo",iSupply.getFishInfo());

        if(!"元".equals(iSupply.getUnit())){
            map.put("quantity",iSupply.getQuantity()+iSupply.getUnit()+"("+iSupply.getUnitWeight()+"斤/"+iSupply.getUnit()+")");
        }else{
            map.put("quantity",iSupply.getQuantity()+iSupply.getUnit());
        }
        map.put("priceInfo",iSupply.getPrice()/100.0+"元/"+iSupply.getUnit());
        map.put("displayTime",iSupply.getDisplayTime());
        map.put("expireTime",iSupply.getExpireTime());
        map.put("state",iSupply.getState());
        map.put("remark",iSupply.getRemark());
        map.put("images",iSupply.getImages());
        map.put("tradePlace",iSupply.getTradePlace());
        //获取属性值
        String sql="select nav.storage,nav.attrValue,na.name,na.sort from nhsx_attribute_value nav inner join nhsx_attribute na on na.id=nav.attrId where nav.pid=?0 and na.storage=?1 order by na.sort desc";
        List<Map> attributeMaps=baseEntityService.getListMapByHQL(sql,iSupply.getId(),iSupply.getStorage());
        map.put("attriButeStr",AttributeTool.getAttributeStr(attributeMaps));

        return JsonTool.returnObjectJsonSuccess(map);
    }

    @RequestMapping(value = "edit", method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,consumes =MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String edit(@RequestBody Map requestMap){
        int id=ParamConvertTool.getParamInt(requestMap,"id");
        String displayTime=ParamConvertTool.getParamStr(requestMap,"displayTime");
        String expireTime=ParamConvertTool.getParamStr(requestMap,"expireTime");
        int state=ParamConvertTool.getParamInt(requestMap,"state");

        String sql="update ISupply set state=?0,displayTime=?1,expireTime=?2 where id=?3";
        if(isupplyService.execHQL(sql,state,displayTime,expireTime,id)==1){
            return JsonTool.returnObjectJsonSuccess(null);
        }else{
            return JsonTool.returnObjectJsonError(null);
        }
    }

    @RequestMapping(value = "updateState", method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,consumes =MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String updateState(@RequestBody Map requestMap){

        String whereIds=ParamConvertTool.getParamStr(requestMap,"ids");

        if(null==requestMap.get("state")){
            return JsonTool.returnObjectJsonError("参数传递错误");
        }

        int state=ParamConvertTool.getParamInt(requestMap,"state");
        if("".equals(whereIds)){
            return JsonTool.returnObjectJsonError("参数传递错误");
        }

        String sql="update ISupply set state=?0 where id in("+whereIds+")";
        if(isupplyService.execHQL(sql,state)==1){
            return JsonTool.returnObjectJsonSuccess(null);
        }else{
            return JsonTool.returnObjectJsonError(null);
        }

    }


}